﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SnmpForm.APConfig
{
    public partial class frmLoginPasswd : frmApConfig
    {
        public frmLoginPasswd(BusinessWrapper.Ap ap):base(ap)
        {
            InitializeComponent();
        }

        public override bool Update(bool bView = true)
        {
            DbWrapper.DbDataTypes.Ap info = m_ap.GetInfo();
            if (bView)
            {
            }
            else
            {
            }
            return true;
        }

        public bool CheckInput()
        {
            bool bValid = false;
            labelError.Text = "";
            string username = textBoxUsername.Text;
            string password = textBoxPasswd.Text;
            string confirm = textBoxConfirmPasswd.Text;
            if (username.Length > 0)
            {
                //用户名是否含有非法字符
                bValid =
                    System.Text.RegularExpressions.Regex.IsMatch(username, @"^[a-zA-Z0-9]+$");
                if (bValid)
                {
                    if (password.Length > 0)
                    {

                        if (password.Equals(confirm))
                        {
                            //密码是否含有非法字符
                            bValid =
                                System.Text.RegularExpressions.Regex.IsMatch(
                                                            password, 
                                                            @"^[a-zA-Z0-9]+$");
                            if (bValid)
                            {
                            }
                            else
                            {
                                labelError.Text = "密码含有非法字符";
                            }
                        }
                        else
                        {
                            bValid = false;
                            labelError.Text = "两次输入的密码不相同";
                        }
                    }
                    else
                    {
                        bValid = false;
                        labelError.Text = "请输入密码";
                    }
                }
                else
                {
                    bValid = false;
                    labelError.Text = "用户名含有非法字符";
                }
            }
            else
            {
                bValid = false;
                labelError.Text = "请输入用户名";
            }
            return bValid;
        }

        public override bool SyncToApCommit()
        {
            DbWrapper.DbDataTypes.Ap info = m_ap.GetInfo();
            bool bSuccess = true;
            string username = textBoxUsername.Text;
            string password = textBoxPasswd.Text;
            bSuccess = m_ap.ChLoginUserAndPasswd(username, password);
            return bSuccess;
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            if (CheckInput())
            {
                Update(false);
                if (SyncToAp())
                {
                    MessageBox.Show("修改成功");
                    Close();
                }
                else
                {
                    MessageBox.Show("修改失败");
                }
            }
        }
    }
}
